﻿using ASP;
using IPMS.BLL;
using Resources;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.Threading;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxControlToolkit;
using IPMS.AccessAuth;
using IPMS.Sort;

public partial class WebPages_edit : Page, IRequiresSessionState
{

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        try
        {
            Auth canView = new Auth(this.Session["UserId"].ToString(), string.Empty, "WebPageMaintenance");
            if (!canView.CanDelete())
            {
                base.Response.Redirect("ErrorPage/NotAllowed.aspx", false);
                return;
            }
        }
        catch (Exception ex)
        {
            base.Response.Redirect("Login.aspx");
        }
        try
            {
                string trueUpdateStr = string.Empty;
                string falseUpdateStr = string.Empty;
                if (Session["Culture"] != null)
                {

                    if (Session["Culture"].ToString() == "zh-TW")
                    {
                        trueUpdateStr = "刪除成功!";
                        falseUpdateStr = "刪除失敗!";

                    }
                    else if (Session["Culture"].ToString() == "zh-CN")
                    {
                        trueUpdateStr = "刪除成功!";
                        falseUpdateStr = "刪除失败!";

                    }
                    else
                    {
                        trueUpdateStr = "Delete succeed!";
                        falseUpdateStr = "Delete failed!";

                    }
                }

                this.ODSPages.DeleteMethod = "Delete";
                this.ODSPages.DeleteParameters["PageName"].DefaultValue = this.txtPageName.Text;
                if (this.ODSPages.Delete() < 0)
                {

                    IPMS.Common.Jscript.AlertAndRedirect(trueUpdateStr, "WebPages.aspx", this.Page);
                }
                else
                {
                    IPMS.Common.Jscript.Alert(falseUpdateStr, this.Page);
                }
                
            }
            catch (Exception ex)
            {
                List<string> lstValues = new List<string>();
            }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string trueUpdateStr = string.Empty;
        string falseUpdateStr = string.Empty;
        string trueInsertStr = string.Empty;
        string falseInsertStr = string.Empty;
        if (Session["Culture"] != null)
        {

            if (Session["Culture"].ToString() == "zh-TW")
            {
                trueUpdateStr = "更新成功!";
				falseUpdateStr = "更新失败!";
				trueInsertStr = "添加成功!";
				falseInsertStr = "添加失敗!";
            }
            else if (Session["Culture"].ToString() == "zh-CN")
            {
                trueUpdateStr = "更新成功!";
				falseUpdateStr = "更新失败!";
				trueInsertStr = "添加成功!";
				falseInsertStr = "添加失败!";
            }
            else
            {
                trueUpdateStr = "Update succeed!";
                falseUpdateStr = "Update failed!";
                trueInsertStr = "Add succeed!";
                falseUpdateStr = "Add failed!";
            }
        }

        if (this.cbForUpdate.Checked)
        {
            this.ODSPages.UpdateMethod = "Update";
            this.ODSPages.UpdateParameters["PageName"].DefaultValue = this.txtPageName.Text;
            this.ODSPages.UpdateParameters["Description"].DefaultValue = this.txtDescription.Text;
            if (this.ODSPages.Update() < 0)
            {

                IPMS.Common.Jscript.AlertAndRedirect(trueUpdateStr, "WebPages.aspx", this.Page);
            }
            else
            {
                IPMS.Common.Jscript.Alert(falseUpdateStr, this.Page);
            }

        }
        else
        {
            try
            {
                this.ODSPages.InsertMethod = "Insert";
                this.ODSPages.InsertParameters["PageName"].DefaultValue = this.txtPageName.Text;
                this.ODSPages.InsertParameters["Description"].DefaultValue = this.txtDescription.Text;
                if (this.ODSPages.Insert() < 0)
                {
                    IPMS.Common.Jscript.AlertAndRedirect(trueInsertStr, "WebPages.aspx", this.Page);
                }
                else
                {
                    IPMS.Common.Jscript.Alert(falseInsertStr, this.Page);
                }
            }
            catch (Exception ex)
            {
            }
        }
        
    }

    protected override void InitializeCulture()
    {
        string sCulture = ConfigurationManager.AppSettings["DefaultCulture"].ToString();
        try
        {
            sCulture = this.Session["Culture"].ToString();
        }
        catch (NullReferenceException)
        {
        }
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(sCulture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(sCulture);
        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e)
    {

        try
        {
            Auth canView = new Auth(this.Session["UserId"].ToString(), string.Empty, "WebPageMaintenance");
            if (!canView.CanUpdate())
            {
                base.Response.Redirect("ErrorPage/NotAllowed.aspx", false);
                return;
            }
        }
        catch (Exception ex)
        {
            base.Response.Redirect("Login.aspx");
        } 
        new Global().ShowError(this.Page, false);
        
            if (!this.Page.IsPostBack)
            {
                ((Label)this.Master.FindControl("lblMstTitle")).Text = " | " + ISitemap.SecurityLevels + " | " + ISitemap.WebPageMaintenance.ToString();

                string pn = Request["pn"];
                if (pn != null && pn != "") {
                    this.txtPageName.Text = Request["pn"];
                    this.txtDescription.Text = Request["descn"];
                    this.txtPageName.Enabled = false;
                    this.cbForUpdate.Checked = true;
                    
                } else {
                    this.btnDelete.Enabled = false;
                    this.cbForUpdate.Checked = false;
                }
                this.btnDelete.Attributes.Add("onClick", "return confirm('" + Messages.DeleteConfirmation.ToString() + "')");
            }
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("WebPages.aspx");
    }
}
